package com.ktgj.customer.controller;

import java.util.List;
import java.io.IOException;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ktgj.common.log.annotation.SignatureValidation;
import com.ktgj.customer.domain.Guarantee;
import com.ktgj.customer.domain.InsuranceRecord;
import com.ktgj.customer.domain.vo.InsuranceDetailsVo;
import com.ktgj.customer.domain.vo.SelGuaranteeVo;
import com.ktgj.customer.domain.vo.TmpGuaranteeVo;
import com.ktgj.customer.service.IGuaranteeService;
import com.ktgj.customer.service.ILoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ktgj.common.log.annotation.Log;
import com.ktgj.common.log.enums.BusinessType;
import com.ktgj.common.security.annotation.PreAuthorize;
import com.ktgj.common.core.web.controller.BaseController;
import com.ktgj.common.core.web.domain.AjaxResult;
import com.ktgj.common.core.utils.poi.ExcelUtil;
import com.ktgj.common.core.web.page.TableDataInfo;

/**
 * 保险
Controller
 * 
 * @author ruoyi
 * @date 2021-07-27
 */
@ControllerAdvice
@RestController
@RequestMapping("/guarantee")
public class GuaranteeController extends BaseController
{
    @Autowired
    private IGuaranteeService guaranteeService;
    @Autowired
    private ILoginService loginService;

    /**
     * 查询保险
列表
     */
    @PreAuthorize(hasPermi = "system:guarantee:list")
    @GetMapping("/list")
    public TableDataInfo list(Guarantee guarantee)
    {
        startPage();
        List<Guarantee> list = guaranteeService.selectGuaranteeList(guarantee);
        return getDataTable(list);
    }

    /**
     * 导出保险
列表
     */
    @PreAuthorize(hasPermi = "system:guarantee:export")
    @Log(title = "保险", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Guarantee guarantee) throws IOException
    {
        List<Guarantee> list = guaranteeService.selectGuaranteeList(guarantee);
        ExcelUtil<Guarantee> util = new ExcelUtil<Guarantee>(Guarantee.class);
        util.exportExcel(response, list, "保险数据");
    }

    /**
     * 获取保险
详细信息
     */
    @PreAuthorize(hasPermi = "system:guarantee:query")
    @GetMapping(value = "/{guaranteeId}")
    public AjaxResult getInfo(@PathVariable("guaranteeId") Long guaranteeId)
    {
        return AjaxResult.success(guaranteeService.selectGuaranteeById(guaranteeId));
    }

    /**
     * 新增保险

     */
    @PreAuthorize(hasPermi = "system:guarantee:add")
    @Log(title = "保险", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Guarantee guarantee)
    {
        return toAjax(guaranteeService.insertGuarantee(guarantee));
    }

    /**
     * 修改保险

     */
    @PreAuthorize(hasPermi = "system:guarantee:edit")
    @Log(title = "保险", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Guarantee guarantee)
    {
        return toAjax(guaranteeService.updateGuarantee(guarantee));
    }

    /**
     * 删除保险

     */
    @PreAuthorize(hasPermi = "system:guarantee:remove")
    @Log(title = "保险", businessType = BusinessType.DELETE)
	@DeleteMapping("/{guaranteeIds}")
    public AjaxResult remove(@PathVariable Long[] guaranteeIds)
    {
        return toAjax(guaranteeService.deleteGuaranteeByIds(guaranteeIds));
    }

    /**
     * 我的-订单列表（保险）
     * */
    //@SignatureValidation
    @PostMapping("/selectInsuranceOrderList")
    public AjaxResult selectInsuranceOrderList(HttpServletRequest request){
        Long customerId;
        try {
            customerId = loginService.getLoginCustomer(request).getCustomerId();
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(guaranteeService.selectGuarnteeByCustomerId(customerId));
    }

    /**
     * 保险信息列表
     * */
    @PostMapping("/guaranteeList")
    public TableDataInfo guaranteeList(@RequestBody TmpGuaranteeVo tmpGuaranteeVo){
        startPage();
        List<InsuranceRecord> insuranceRecordList;
        try {
            insuranceRecordList = guaranteeService.guaranteeList(tmpGuaranteeVo);
        } catch (Exception e){
            return null;
        }

        return getDataTable(insuranceRecordList);
    }

    /**
     * 查询待修改保险信息
     * */
    @PostMapping("/selGuarantee")
    public AjaxResult selGuarantee(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        SelGuaranteeVo selGuaranteeVo;
        try {
            selGuaranteeVo = guaranteeService.selGuarantee(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(selGuaranteeVo);
    }

    /**
     * 修改保险信息
     * */
    @PostMapping("/upGuarantee")
    public AjaxResult upGuarantee(@RequestBody SelGuaranteeVo selGuaranteeVo){
        int i;
        try {
            i = guaranteeService.upGuarantee(selGuaranteeVo);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        if (i > 0){
            return AjaxResult.success("修改成功");
        }
        else {
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 保险详情
     * */
    @PostMapping("/insuranceDetails")
    public AjaxResult insuranceDetails(@RequestBody Map<String, Object> map){
        Long id = Long.valueOf(map.get("id").toString());
        InsuranceDetailsVo insuranceDetailsVo;
        try {
            insuranceDetailsVo = guaranteeService.insuranceDetails(id);
        } catch (Exception e){
            return AjaxResult.error(e.getMessage());
        }
        return AjaxResult.success(insuranceDetailsVo);
    }
}
